package capaGrafica;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import capaNegocio.PedidoCliente;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JList;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class VentanaVerPedidos extends JDialog {


	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JComboBox<Fecha> combo;
	private JList<String> list;
	
	public static void main(String[] args) {
		
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e){
			System.out.println("Error setting native LAF: "+e);
		}
		
		try {
			VentanaVerPedidos dialog = new VentanaVerPedidos();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	

	public VentanaVerPedidos() {
		setTitle("Recorridos por fecha");
		setBounds(100, 100, 559, 415);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewlabel = new JLabel("Selecionar fecha para ver recorrido");
			lblNewlabel.setBounds(44, 12, 260, 34);
			contentPanel.add(lblNewlabel);
		}
		
		combo = new JComboBox<Fecha>();
		
		// evento combo box
		combo.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				
				cargarLista();
			}

			
		});
		combo.setBackground(Color.WHITE);
		combo.setEditable(false);
		combo.setBounds(94, 65, 159, 24);
		contentPanel.add(combo);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(324, 61, 205, 262);
		contentPanel.add(scrollPane);
		
		list = new JList<String>();
		list.setFont(new Font("Dialog", Font.BOLD, 14));
		scrollPane.setViewportView(list);
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				// evento boton ok
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
						Fecha fecha = (Fecha)combo.getSelectedItem();
						MainForm.setFechaRecorido(fecha);
						MainForm.mostrarRecorrido();
						volverAVentanaAnterior();
						
					}
				});
				okButton.setBackground(Color.WHITE);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				//evento boton cancel
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						volverAVentanaAnterior();
					}
				});
				cancelButton.setBackground(Color.WHITE);
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		cargarComboBox();
		cargarLista();
		
	}
	
	// metodos
	private void cargarComboBox() {
		ArrayList<Fecha> otro = sinRepetidosYOrdenado();

		if(otro.size() != 0){
			for(Fecha f: otro){
				combo.addItem(f);
			}
		}
	}

	private ArrayList<Fecha> sinRepetidosYOrdenado(){

		@SuppressWarnings("static-access")
		ArrayList<PedidoCliente> arr = MainForm.getInteligencia().getListaPedidos();

		ArrayList<Fecha> ret  =  new ArrayList<Fecha>();
		if(arr.size() != 0){

			for(int i = 0 ; i < arr.size(); i++){
				if(!ret.contains(arr.get(i).getFecha())){
					ret.add(arr.get(i).getFecha());	
				}
			}
		}



		Collections.sort(ret);
		return ret;

	}
	private void cargarLista() {
		
		@SuppressWarnings("static-access")
		ArrayList<PedidoCliente> arr =  MainForm.getInteligencia().getListaPedidos();
		ArrayList<String> ret = new ArrayList<String>();
		
		 for(PedidoCliente e : arr){
			 if(e.getFecha().equals((Fecha)combo.getSelectedItem()) &&
					 !e.dameNombreCliente().equals("Facultad (Punto Partida)")){
				 ret.add(e.dameNombreCliente());
				 
			 }
		 }
		 llenarLista(ret);
	}

	
	private void llenarLista(ArrayList<String> ret) {

		DefaultListModel<String> listModel = new DefaultListModel<String>();
		
		if(ret.size() != 0){
			
			for(int i=0; i <ret.size(); i++) {
				listModel.add(i, ret.get(i));
			}
			list.setModel(listModel);

		}
	}
	

	private void volverAVentanaAnterior() {
		MainForm.habilitarVentana(true);
		setVisible(false);
		dispose();
	}
}
